package cn.edu.zhku.citrusmonitor.baseinfo.technology.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.baseinfo.technology.exception.TechnologyException;
import cn.edu.zhku.citrusmonitor.baseinfo.technology.service.TechnologyService;
import cn.edu.zhku.citrusmonitor.baseinfo.technology.service.TechnologyServiceImpl;
import cn.edu.zhku.citrusmonitor.constant.Constant;
import cn.edu.zhku.citrusmonitor.constant.TechnologyCategory;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbTechnology;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.FileOperationUtil;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.PageBean;
import cn.edu.zhku.citrusmonitor.util.PrintMessageUtil;

@WebServlet("/technology.do")
public class TechnologyServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;

	// 添加
	public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TbTechnology technology = new TbTechnology();
		Map<String, String[]> map = null;
		try {
			map = FileOperationUtil.upload(request, Constant.TECHNOLOGY_IMG_PATH);
			ConvertUtils.register(new DateLocaleConverter(), Date.class);
			BeanUtils.populate(technology, map);
			TechnologyService ts = new TechnologyServiceImpl();
			technology.setTechnologyId(UUID.randomUUID().toString());
			boolean res = ts.addTechnology(technology);
			return PrintMessageUtil.printMessage(response, res);	// 返回结果
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		} catch (Exception e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("服务器繁忙!"));
		}		
	}
	
	// 修改
	public String modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TbTechnology technology = new TbTechnology();
		try {
			ConvertUtils.register(new DateLocaleConverter(), Date.class);
			BeanUtils.populate(technology, request.getParameterMap());
			ConvertUtils.register(new DateLocaleConverter(), Date.class);
			TechnologyService ts = new TechnologyServiceImpl();
			boolean res = ts.modifyTechnology(technology);
			return PrintMessageUtil.printMessage(response, res);
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		} catch (Exception e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("服务器繁忙!"));
		}
	}
	
	// 删除
	public String remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String technologyId = request.getParameter("technologyId");
		TechnologyService ts = new TechnologyServiceImpl();
		try {
			boolean res = ts.removeTechnology(technologyId);
			return PrintMessageUtil.printMessage(response, res);
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 批量删除
	public String removeTechnologys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		TechnologyService ts = new TechnologyServiceImpl();
		boolean result = false;
		try {
			result = ts.removeTechnologysByIds(ids);
			return PrintMessageUtil.printMessage(response, result);	
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 根据id查询，返回TbTechnology类
	public String findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String technologyId = request.getParameter("technologyId");
		TechnologyService ts = new TechnologyServiceImpl();
		TbTechnology technology = null;
		try {
			technology = ts.findTechnologyById(technologyId);
			return PrintMessageUtil.printMessageWithDateFormat(response, technology, "yyyy-MM-dd HH:mm:ss");
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 根据id查询，返回Bean类
	public String getTechnologyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String technologyId = request.getParameter("id");
		TechnologyService ts = new TechnologyServiceImpl();
		Bean bean = null;
		try {
			bean = ts.getTechnologyById(technologyId);
			return JSON.toJSONStringWithDateFormat(bean, "yyyy-MM-dd HH:mm:ss", SerializerFeature.EMPTY);
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 根据类别查询，返回List
	public String findByCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String category = request.getParameter("category");
		TechnologyService ts = new TechnologyServiceImpl();
		List<TbTechnology> list = null;
		try {
			list = ts.findTechnologyByCategory(category);
			return PrintMessageUtil.printListMessageWithDateFormat(response, list, Constant.DEFAULT_DATEFORMAT);
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 分页查询所有
	public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return fySearch(request, response, null);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	// 根据类别获取指定条数信息
	public String getTechnologys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TechnologyService ts = new TechnologyServiceImpl();
		String size = request.getParameter("pageSize");
		String category = request.getParameter("category");
		if(size == null) {
			size = Constant.DEFAULT_PAGESIZE;
		}
		List<TbTechnology> tList = null;
		try {
			tList = ts.findTechnologyByCategoryLimit(Integer.parseInt(size) ,category);
			return PrintMessageUtil.printListMessageWithDateFormat(response, tList, Constant.DEFAULT_DATEFORMAT);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 根据类别分页查询，返回PageBean，后台使用
	public String fySearchByCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String category = request.getParameter("category");
		try {
			return fySearch(request, response, category);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	// 返回种植技术分页列表
	public String findPlant(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return getTechnologyPageList(request, response, TechnologyCategory.ZHONGZHI.getName());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 返回施肥技术分页列表
	public String findFertilizerTechnology(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return getTechnologyPageList(request, response, TechnologyCategory.SHIFEI.getName());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 返回施药技术分页列表
	public String findMedicineTechnology(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return getTechnologyPageList(request, response, TechnologyCategory.SHIYAO.getName());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 返回防虫技术分页列表
	public String findPestTechnology(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return getTechnologyPageList(request, response, TechnologyCategory.FANGCHONG.getName());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 返回防害技术分页列表
	public String findDiseaseTechnology(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return getTechnologyPageList(request, response, TechnologyCategory.FANGBING.getName());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (TechnologyException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 提供前台分页展示数据，需要提供类别
	private String getTechnologyPageList(HttpServletRequest request, HttpServletResponse response, String category) throws IOException, NumberFormatException, TechnologyException {
		String pageSize = request.getParameter("pageSize");
		if(pageSize==null) {
			pageSize = Constant.DEFAULT_PAGESIZE;
		}
		String currentPage = request.getParameter("currentPage");
		TechnologyService tService = new TechnologyServiceImpl();
		if(category != null) {
			PageBean<Bean> pageBean = tService.findTechnologyPageByCategory(Integer.parseInt(pageSize), Integer.parseInt(currentPage), category);
			return JSON.toJSONStringWithDateFormat(pageBean, Constant.DEFAULT_DATEFORMAT, SerializerFeature.EMPTY);
		}else {
			return null;
		}
	}
	
	// 分页查询
	private String fySearch(HttpServletRequest request, HttpServletResponse response, Object condition) throws IOException, NumberFormatException, TechnologyException {
		String pageSize = request.getParameter("pageSize");
		if(pageSize==null) {
			pageSize = Constant.DEFAULT_PAGESIZE;
		}
		String currentPage = request.getParameter("currentPage");
		TechnologyService tService = new TechnologyServiceImpl();
		PageBean<TbTechnology> pageBean = null;
		if(condition != null) {
			pageBean = tService.findTechnologyByCategoryLike(Integer.parseInt(pageSize), Integer.parseInt(currentPage), (String)condition);
		}else{
			pageBean = tService.fySearchAll(Integer.parseInt(pageSize), Integer.parseInt(currentPage));
		}
		return JSON.toJSONStringWithDateFormat(pageBean, Constant.DEFAULT_DATEFORMAT, SerializerFeature.EMPTY);
	}
	
	
	
}
